package com.mall.biz.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.AddressService;
import com.mall.pojo.Address;
import com.mall.pojo.AddressExample;
import com.mall.pojo.AddressExample.Criteria;
import com.mall.pojo.User;

@Service
public class AddressBizImpl {
	@Autowired
	private AddressService addressService;

	public List<Address> getAddressListByUserAndOnlyKey(User user, Integer onlyKey,Integer type) {
		AddressExample example = new AddressExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		criteria.andCommOnlyKeyEqualTo(onlyKey);
		criteria.andStatusNotEqualTo(-8);//删除的不展示
		if(type != null && type >0){
			criteria.andTypeEqualTo(type);
		}
		List<Address> addressList = this.addressService.getAddressLsitByExample(example);
		return addressList;
	}
	
	public List<Address> getAddressListByUserAndstatus(Long userId, Integer onlyKey,Integer type,Integer status) {
		AddressExample example = new AddressExample();
		Criteria criteria = example.createCriteria();
		if(userId != null && userId >= 0){
			criteria.andUserIdEqualTo(userId);
		}
		if(onlyKey != null){
			criteria.andCommOnlyKeyEqualTo(onlyKey);
		}
		if(status != null){
			criteria.andStatusEqualTo(status);
		}else{
			criteria.andStatusNotEqualTo(-8);//删除的不展示
		}
		if(type != null && type > 0){
			criteria.andTypeEqualTo(type);
		}
		List<Address> addressList = this.addressService.getAddressLsitByExample(example);
		return addressList;
	}
	
	//获取地址中的第一条
	public Address getFirstAddress(List<Address> addressList){
		if(addressList != null && addressList.size() > 0){
			return addressList.get(0);
		}
		return null;
	}
	
	
	public String firstPartAddress(String address){
		String firstPartAddress = null;
		if(address.contains("区")){
			firstPartAddress = address.split("区")[0]+"区";
			return firstPartAddress;
		}
		return address;
	}
	
	public String secondPartAddress(String address){
		String secondPartAddress = null;
		if(address.contains("区")){
			secondPartAddress = address.split("区")[1];
			return secondPartAddress;
		}
		return address;
	}
	

}
